/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flange;

import flange.functions.Function2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *
 * @author Сергей
 */
public class Graph2D extends JPanel {
    private Function2D f;
    private String title;

    public Graph2D() {
        super();
        this.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.clearRect(0, 0, this.getWidth(), this.getHeight());
        int ic = 0, icdiver = 10;
        for(int i = 0; i < this.getWidth(); i += 10) {
            if (ic == 0)
                g.setColor(Color.gray);
            else
                g.setColor(Color.lightGray);
            g.drawLine(i, 0, i, this.getHeight() - 1);
            if (ic == 0) {
                g.setColor(Color.DARK_GRAY);
                String s = String.valueOf(Math.round(f.getXbyX(i)));
                g.drawChars(s.toCharArray(), 0, s.length(), i+2, this.getHeight()-2);
            }
            ic ++; if (ic >= icdiver) ic = 0;
        }
        ic = 0;
        for(int i = this.getHeight() - 1; i >= 0; i -= 10) {
            if (ic == 0)
                g.setColor(Color.gray);
            else
                g.setColor(Color.lightGray);
            g.drawLine(0,i, this.getWidth() - 1, i);
            if (ic == 0) {
                g.setColor(Color.DARK_GRAY);
                String s = String.valueOf(Math.round(f.getYbyY(i)));
                g.drawChars(s.toCharArray(), 0, s.length(), 2, i - 1);
            }
            ic ++; if (ic >= icdiver) ic = 0;
        }
        g.setColor(Color.blue);
        int x = 0, y = (int) f.getY(0);
        for(int i = 1; i < this.getWidth(); i++) {
            int ny = (int) f.getNormY(i);
            g.drawLine(x, y, i, ny);
            y = ny;
            x = i;
        }
        g.setColor(Color.red);
        g.drawChars(title.toCharArray(), 0, title.length(), 20, 20);

    }
    public void setFunction(Function2D f) {
        this.f = f;
        f.setPanelSize(this.getWidth(), this.getHeight());
    }
    public void setTitle(String title) {
        this.title = title;
    }
}
